<template>
  <div class="hFull flex column pra speech-recognition-questions-body">
    <div class="equal">
      <div class="radioBg">
        <!-- 题目 -->
        <div class="head">
          <div class="flex headBox aicenter jccenter">
            <div class="tips mr10"></div>
            <div class="equal fz16 overflow-y qtText">{{question.guide}}</div>
          </div>
        </div>
        <!-- 问题 -->
        <div class="question">
          <div class="define pra">
            <input type="button" class="defineBtn" @click="submit()" />
          </div>
        </div>
      </div>
    </div>
    <!-- 弹出 -->
    <div class="black none"></div>
    <div class="error none">
      <div class="flex aicenter jccenter errorImg">
        <img src="../assets/images/errorImg.png" alt />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: ["submitAnswer", "question", "mp3Addr"],
  data() {
    return {
      answer: null,
      mp3Index: 0,
      buttonUse: 'play',
      audio:null,

    };
  },
  created() {
    console.log(this.question);
    
  },
  methods: {
    play() {
      var root = this;
      this.audio = new Audio();
      root.audio.src = this.mp3Addr + this.question.correct_mp3;
      root.audio.loop = false;
      root.audio.addEventListener('ended', function () {
        if(root.mp3Index>=root.question.stem_mp3.length){
          root.mp3Index=0;
          this.buttonUse='play';
        }else{
            root.buttonUse='answer';
            setTimeout(function(){
              if(root.audio!=null){
                root.audio.src = root.mp3Addr + root.question.stem_mp3[root.mp3Index++];
                root.audio.play();
              }
            }, 2000);
        }
      },false);
      root.audio.play();
    },
    submit() {
      if(this.buttonUse==='answer'){
        this.audio=null;
        this.submitAnswer([this.question.stem[--this.mp3Index]],[this.question.stem_mp3[--this.mp3Index]]);
        this.buttonUse='play';
        this.mp3Index=0;
      }else if(this.buttonUse==='play'){
        this.play();
      }
    },
  },
};
</script>
<style lang="css">
.speech-recognition-questions-body {
  width: 100%;
  position: relative;
  background: url(../assets/images/pic-04.png) no-repeat center center;
  background-size: 100% 100%;
}
.head {
  position: absolute;
  top: 0.2rem;
  left: 0;
  right: 0;
  padding: 0 0.5rem;
}
.headBox {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 0.1rem;
  border: 0.1rem solid #fff;
}
.tips {
  width: 0.38rem;
  height: 0.33rem;
  display: block;
  background: url(../assets/images/newsIcon.png) no-repeat;
  background-size: 100%;
}
.question {
  width: 20%;
  position: absolute;
  top: 20%;
  left: 45%;
  bottom: 1%;
  right: 35%;
  text-align: center;
  display: flex;
  justify-content: flex-end;
  flex-direction: column;
}
.submit {
  width: 0.98rem;
  height: 0.98rem;
  background: url(../assets/images/submitBtn.png) no-repeat center center;
  background-size: 100% 100%;
  cursor: pointer;
  margin-left: 0.2rem;
}
.qtText {
  height: 0.5rem;
  display: flex;
  justify-content: center;
  flex-direction: column;
}
.black {
  background: rgba(0, 0, 0, 0.7);
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0px;
  top: 0px;
  bottom: 0;
  right: 0;
  z-index: 4;
}
.error {
  position: fixed;
  width: 60%;
  left: 20%;
  bottom: 20%;
  right: 20%;
  top: 20%;
  z-index: 10;
}
.errorImg {
  width: 100%;
  height: 100%;
}
.errorImg img {
  width: auto;
  max-width: 100%;
}
.define {
  width: 1.14rem;
  height: 2.64rem;
  background: url(../assets/images/btnbg.png) no-repeat center center;
  background-size: 100% 100%;
}
.defineBtn {
  width: 0.94rem;
  height: 0.94rem;
  background: url(../assets/images/defineBtn.png) no-repeat center center;
  background-size: 100% 100%;
  border: none;
  cursor: pointer;
  position: absolute;
  top: 0.1rem;
  left: 0.1rem;
}
.pob {
  position: absolute;
  right: 0.2rem;
  bottom: 0.2rem;
}
</style>